/**
 * Copyright (C) 2012 Oleg Anastassov <o.anastasov@gmail.com>
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package kz.kstu.ent.desktop.components.model;

import kz.kstu.ent.business.entity.Identifiable;

import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import java.util.ArrayList;
import java.util.List;


/**
 * @author Oleg Anastassov
 */
public abstract class AbstractSimpleTableModelProxy<T extends Identifiable> implements TableModel, Sortable {

    private SimpleTableModal tableModal;

    public AbstractSimpleTableModelProxy() {
        tableModal = new SimpleTableModal();
        tableModal.setColumnNames(getColumnNames());
    }

    public void setData(List<T> dataList) {
        List<Object[]> rows = new ArrayList<Object[]>();
        for (T transportation : dataList) {
            Object[] row = createRow(transportation);
            rows.add(row);
        }
        tableModal.setData(rows);
    }

    public void addRow(T obj) {
        Object[] row = createRow(obj);
        tableModal.addRow(row);
    }

    @Override
    public int getRowCount() {
        return tableModal.getRowCount();
    }

    @Override
    public int getColumnCount() {
        return tableModal.getColumnCount();
    }

    @Override
    public String getColumnName(int columnIndex) {
        return tableModal.getColumnName(columnIndex);
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return tableModal.getColumnClass(columnIndex);
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return tableModal.isCellEditable(rowIndex, columnIndex);
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return tableModal.getValueAt(rowIndex, columnIndex);
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        tableModal.setValueAt(aValue, rowIndex, columnIndex);
    }

    @Override
    public void addTableModelListener(TableModelListener l) {
        tableModal.addTableModelListener(l);
    }

    @Override
    public void removeTableModelListener(TableModelListener l) {
        tableModal.removeTableModelListener(l);
    }

    @Override
    public void sortByColumn(int columnIndex) {
        tableModal.sortByColumn(columnIndex);
    }

    public List<Object[]> getData() {
        return tableModal.getData();
    }

    public void clear() {
        tableModal.clear();
    }

    protected abstract Object[] createRow(T obj);

    protected abstract String[] getColumnNames();

}
